{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 18\n",
    "\n",
    "# 二元高斯函数积分\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28e07edf-9705-45c3-9a49-219e1dd350c0",
   "metadata": {},
   "source": [
    "这段代码通过积分计算高斯函数\n",
    "\n",
    "$$\n",
    "f(x, y) = e^{-x^2 - y^2}\n",
    "$$\n",
    "\n",
    "在不同区域上的累积面积和总体面积。\n",
    "\n",
    "1. **累积积分**：首先，代码计算了从 $(-\\infty, -\\infty)$ 到 $(x, y)$ 的累积积分\n",
    "\n",
    "   $$\n",
    "   F(x, y) = \\int_{-\\infty}^x \\int_{-\\infty}^y e^{-u^2 - v^2} \\, dv \\, du\n",
    "   $$\n",
    "\n",
    "   该积分表示从左下角到点 $(x, y)$ 之间区域内函数 $f(x, y)$ 的累积面积。累积积分在概率论中常用于描述二维正态分布中的累积概率，表示该区域内的概率密度累积。\n",
    "\n",
    "2. **体积积分**：其次，代码计算了 $f(x, y)$ 在整个 $xy$ 平面上的积分，即从 $(-\\infty, -\\infty)$ 到 $(\\infty, \\infty)$ 的积分。这相当于计算 $f(x, y)$ 在无穷大区域内的总面积（或体积）：\n",
    "\n",
    "   $$\n",
    "   \\int_{-\\infty}^{\\infty} \\int_{-\\infty}^{\\infty} e^{-x^2 - y^2} \\, dx \\, dy = \\pi\n",
    "   $$\n",
    "\n",
    "   此结果表明，在整个平面上积分，二维高斯函数的面积是有限的，为 $\\pi$。这表示标准二维正态分布的总概率密度，揭示了高斯分布的收敛特性。代码通过计算展示了该函数的局部累积行为和总体特性。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a09643a6-0e97-4823-8ff8-7173cacfdae9",
   "metadata": {},
   "source": [
    "## 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e457bcfd-9e71-4224-b5bf-93c69be2d5a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy.abc import x, y, s, t  # 定义符号变量 x, y, s, t\n",
    "from sympy import *  # 导入符号计算库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ece5b932-2c75-4b54-a107-47e22a2e17f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e^{- x^{2} - y^{2}}$"
      ],
      "text/plain": [
       "exp(-x**2 - y**2)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_xy = exp(- x**2 - y**2)  # 定义函数 f(x, y) = exp(-x^2 - y^2)\n",
    "f_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "661e667b-7768-47b8-a2f2-50d2b1b0f34a",
   "metadata": {},
   "source": [
    "## 计算累积积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ce6b2d77-460f-46d9-90b9-24dadd8af54f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\pi \\operatorname{erf}{\\left(x \\right)} \\operatorname{erf}{\\left(y \\right)}}{4} + \\frac{\\pi \\operatorname{erf}{\\left(x \\right)}}{4} + \\frac{\\pi \\operatorname{erf}{\\left(y \\right)}}{4} + \\frac{\\pi}{4}$"
      ],
      "text/plain": [
       "pi*erf(x)*erf(y)/4 + pi*erf(x)/4 + pi*erf(y)/4 + pi/4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f_x_y_double_integrate = integrate(f_xy, (y, -oo, y), (x, -oo, x))  # 计算从 (-∞, -∞) 到 (x, y) 的累积积分\n",
    "f_x_y_double_integrate"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57114b91-fd59-466c-8dd9-3c945ad862fb",
   "metadata": {},
   "source": [
    "## 计算体积积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8b1e2579-5f39-4b39-8993-85c4f8430465",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi\n"
     ]
    }
   ],
   "source": [
    "f_x_y_volume = integrate(f_xy, (y, -oo, oo), (x, -oo, oo))  # 计算整个平面上的积分（对应体积）\n",
    "print(f_x_y_volume)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
